package com.tgpms.vforms.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.vforms.service.EngineeringService;

@Service
public class EngineeringServiceImpl implements EngineeringService {

	@Resource
    private VformInterface vformInterface;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> selEngineeringTreeList(String dataowner,String entityItem) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		JSONObject json = new JSONObject();
		if(!Common.isEmpty(entityItem)) {
			JSONObject json1 = new JSONObject();
			json1.put("ENTITY_ITEM=", entityItem);
			Result<Object> result1;
			if (StringUtils.isEmpty(dataowner))
				result1 = vformInterface.find("D2003", json1.toJSONString(), "1", "10000", "N", "N", "");
			else
				result1 = vformInterface.find("D2003", json1.toJSONString(), "1", "10000", "Y", "Y", dataowner);
			List<Map<String,Object>> plist = JSONArray.parseObject(result1.getData().toString(), List.class);
			if(plist!=null && plist.size()>0) {
				json.put("RESPONSIBLE_PERSON=", plist.get(0).get("POSITION").toString());
			}else {
				return list;
			}
		}
		Result<Object> result1;
		if (StringUtils.isEmpty(dataowner))
			result1 = vformInterface.find("D1021", json.toJSONString(), "1", "10000", "N", "N", "");
		else
			result1 = vformInterface.find("D1021", json.toJSONString(), "1", "10000", "Y", "Y", dataowner);
		System.out.println("D1021工程查询返回的结果为："+result1.getData());
		List<Map<String,Object>> prolist = JSONArray.parseObject(result1.getData().toString(), List.class);
		Result<Object> result2;
		if (StringUtils.isEmpty(dataowner))
			result2 = vformInterface.find("D1023", "", "1", "10000", "N", "N", "");
		else
			result2 = vformInterface.find("D1023", "", "1", "10000", "Y", "Y", dataowner);
		System.out.println("D1023子工程查询返回的结果为："+result2.getData());
		List<Map<String,Object>> subprolist = JSONArray.parseObject(result2.getData().toString(), List.class);
		Result<Object> result32;
		if (StringUtils.isEmpty(dataowner))
			result32 = vformInterface.find("D1180", "", "1", "10000", "N", "N", "");
		else
			result32 = vformInterface.find("D1180", "", "1", "10000", "Y", "Y", dataowner);
		System.out.println("D1180CWP查询返回的结果为："+result32.getData());
		List<Map<String,Object>> cwplist = JSONArray.parseObject(result32.getData().toString(), List.class);
		//循环拼接树形数据
		prolist.forEach(a -> {
			List<Map<String,Object>> subs = new ArrayList<Map<String,Object>>();
			subprolist.stream()
				.filter(b -> b.get("PROJECT").toString().equals(a.get("PROJECT").toString()))
				.forEach(b -> {
					List<Map<String,Object>> cwps = new ArrayList<Map<String,Object>>();
					cwplist.stream()
						.filter(c -> c.get("SUB_PROJECT").toString().equals(b.get("SUB_PROJECT").toString()))
						.forEach(c -> {
							cwps.add(c);
						});
					b.put("CWPINFO", cwps);
					subs.add(b);
				});
			a.put("SUBPROJECTINFO", subs);
			list.add(a);
		});
		return list;
	}

}
